package com.study.example.ex02;

import java.io.IOException;
import java.io.Reader;

/**
* @Name: MyBufferedReader
* @Description: 自定义高效字符读取流：模拟BufferedReader的readLine()方法的功能
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-1-16 下午3:29:07 
* @Version: V1.0
 */
public class MyBufferedReader {

	private Reader r ;

	public MyBufferedReader(Reader r) {
		this.r = r;
	} 
	
	public String readLine() throws IOException {
		StringBuilder sb = new StringBuilder() ;
		int ch = 0 ;
		while((ch = r.read()) != -1) {
			if(ch == '\r') {
				continue ;
			} 
			if(ch == '\n') {
				return sb.toString() ;
			} else {
				sb.append((char)ch) ;
			}
		}
		if(sb.length() > 0) {
			return sb.toString() ;
		}
		return null ;
	}
	
	public void close() throws IOException {
		this.r.close() ;
	}
	
	
	
}
